<?php
/**
 * 服务窗应用激活开发者模式--应用网关
 * User: zym(wika)
 * Date: 2016/12/1
 * Time: 11:41
 */
class AlipayGatewayOkAction extends CommonAction
{
    public $dataAlipay;

    public function _initialize()
    {
        $this->dataAlipay = array(
            //应用ID,您的APPID。
            'app_id' => " 2017092908987613",

            //商户私钥，您的原始格式私钥,一行字符串
            'merchant_private_key' => "MIIEpQIBAAKCAQEAtohe/vm4u0WW+dKjDSzIjWlL27lPQ5ggqvVW4JOKiudNkCc1ZcKOVwYXdaJXux2B72TDQJ8kNwaRCwWblTRMEItdgu8CZDtcbFbb9oZKjWqDNRdEqURuadmoUCMqgQ/K60JjmInSuaVqEQ78lXO3V2ujoavlo7N6C5u1lOcxCZ2OYSfL0IZb0DR7fvcmNnCS3k+qXTf5mHhzLHrOGzOQ8dVVi6pc8gbGruSdrSzsUnWYytfUhutbS4+iXxSmg+u/oT5ZPM+zdcvIIiR+KmBA2VsyqtoNGvA8cn5vkWIneJOAWyyxa5NIp34SdV6Rz5oESPnF1zHmjuRkXxRP+QHVHQIDAQABAoIBAQCHTp3Eus7Sm6pn9FmD2a+tC+H8teho/esxb36AKT6o6v9XHYl6rncfUVewjrAj96KrF/GZBJ8u3wJDvltvhf3gwb6G4a/PJyXC9bxGiZPYMp2ijGYMZ7rFYmBfT0Ihu9g6/tKfb4MytMjkbkyUaNv+xL7eiHNJHzEYS8uV3q8+JTfu8ZDmDK0Z16ffhv09SCXzRBEKSu9n0x+rm6rb7mUBJjLGKGRgrZaEsjAxHX276bQ946UobOGN0+7nfrUOfUXuXilYyRoKOD+VlRge1sFYyqRBrbRLb7oMEfwufjjnXY+naDAgfWoJUcOO3XeUkZAf8M2kKw1P5BVnu8yYrDtNAoGBAPBOWqKi/YeDZVyMjh0JjptQdPZubz6nZgiRCwait4NENCBHivqhOL2Dj3l7LKbYf/DMD8HrvTSFZCamr2ul07gZsiQ4NitCFvKfGj87USat7R/u7+QEYApv69yXnRcSuopS9krZCprf+P2VnAOmcN1Gm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cvIIiR+KmBA2VsyqtoNGvA8cn5vkWIneJOAWyyxa5NIp34SdV6Rz5oESPnF1zHmjuRkXxRP+QHVHQIDAQAB",

            //支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
            'alipay_public_key' => "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmDCJP0ke6q3AJbjb3TIW02X+wPd81P+hnYxTpNxoJ1wJ/LU64fF7218qoi8G3Frjio5wszljVREcpFdvQGs+SJ+NnjRdKJsPMFB/UVV/oekgc62dicn6wGMj1Yv+i5oCZvnlOaNJY8imDOwSHl097uVToG+dKac2F86HuaOYD4KnZ82jmBohzTivwlgNBEnlPT+nSFE4uGESKtKl8OegPwWoYtLHwkUo7XWskeVUzalCbgXZcsmppyjoEw6DJHbmYYKk7GAxQl6CRAqR+ESmcVdmYzQh24cA7kGA4MT9HbsZWYNovYuEupEhmRtM9G6/OL5j3Ha+unn7hE0/b5LwKwIDAQAB",


            //编码格式只支持GBK。
            'charset' => "GBK",

            //支付宝网关
            'gatewayUrl' => "https://openapi.alipay.com/gateway.do",

            //签名方式
            'sign_type'=>"RSA2"
        );
    }

    /**
     * 此文件未对接支付宝服务器的网关文件，将此文件的访问路径填入支付宝服务窗的开发中验证网关的页面中。
     * 次文件接收支付宝服务器发送的请求
     */
    public function index()
    {
        wLog("index:enter", 'AlipayMessage');
        $sign = $_REQUEST['sign'];
        $sign_type = $_REQUEST['sign_type'];
        $biz_content = $_REQUEST['biz_content'];
        $service = $_REQUEST['service'];
        $charset = $_REQUEST['charset'];
        wLog("request:".var_export($_REQUEST, true), 'AlipayMessage');
        if (empty ($sign) || empty ($sign_type) || empty ($biz_content) || empty ($service) || empty ($charset)) {
            echo "some parameter is empty.";
            exit ();
        }

        // 收到请求，先验证签名
        vendor('Alipay.AopClientNew');
        $as = new AopClient();
        $as->rsaPrivateKey = $this->dataAlipay['merchant_private_key'];
        $merchant_public_key = $this->dataAlipay['merchant_public_key'];
        $response_xml = "<success>true</success><biz_content>" . $merchant_public_key . "</biz_content>";
        $mysign = $as->alonersaSign($response_xml, $this->dataAlipay['merchant_private_key'], $this->dataAlipay['sign_type'],false);
        wLog("mysign:".$mysign, 'AlipayMessage');
        $return_xml = "<?xml version=\"1.0\" encoding=\"" . $this->dataAlipay['charset'] . "\"?><alipay><response>" . $response_xml . "</response><sign>" . $mysign . "</sign><sign_type>" . $this->dataAlipay['sign_type'] . "</sign_type></alipay>";
        wLog("return_xml:".$return_xml, 'AlipayMessage');
        echo $return_xml;
        exit ();
        /*//$as->alipayrsaPublicKey = $this->dataAlipay['alipay_public_key'];
        wLog("post:".var_export($_POST,true), 'AlipayMessage');
        //$sign_verify = $as->rsaCheckV2($_POST, $this->dataAlipay['alipay_public_key'], $this->dataAlipay['sign_type']);
        wLog("sign_verify:".$sign_verify, 'AlipayMessage');
        if (!$sign_verify) {
            wLog("sign qianming verfiy fail", 'AlipayMessage');
            // 如果验证网关时，请求参数签名失败，则按照标准格式返回，方便在服务窗后台查看。
            if ($service == "alipay.service.check") {
                $this->verifygw(false, $as, $biz_content);
            } else {
                echo "sign verfiy fail.";
                wLog(var_export($_REQUEST, true), 'AlipayMessage');
            }
            exit ();
        }

        // 验证网关请求
        if ($service == "alipay.service.check") {
            $this->verifygw(true, $as, $biz_content);
        } else if ($service == "alipay.mobile.public.message.notify") {
            // 处理收到的消息

        }*/
    }


    public function verifygw($is_sign_success, $as, $biz_content)
    {
        $xml = simplexml_load_string($biz_content);
        $EventType = (string)$xml->EventType;
        if ($EventType == "verifygw") {
            $as->rsaPrivateKey = $this->dataAlipay['merchant_private_key'];
            //$merchant_public_key = file_get_contents($this->dataAlipay['merchant_public_key_one']);
            $merchant_public_key = $this->dataAlipay['merchant_public_key'];
            if ($is_sign_success) {
                $response_xml = "<success>true</success><biz_content>" . $merchant_public_key . "</biz_content>";
            } else {
                $response_xml = "<success>false</success><error_code>VERIFY_FAILED</error_code><biz_content>" . $merchant_public_key . "</biz_content>";
            }
            $mysign = $as->alonersaSign($response_xml, $this->dataAlipay['merchant_private_key'], $this->dataAlipay['sign_type'],false);
            wLog("mysign:".$mysign, 'AlipayMessage');
            $return_xml = "<?xml version=\"1.0\" encoding=\"" . $this->dataAlipay['charset'] . "\"?><alipay><response>" . $response_xml . "</response><sign>" . $mysign . "</sign><sign_type>" . $this->dataAlipay['sign_type'] . "</sign_type></alipay>";
            wLog("return_xml:".$return_xml, 'AlipayMessage');
            echo $return_xml;
            exit ();
        }
    }
}

